<?php
include("http://02d2e0a.netsolhost.com/includes/head.inc");
// include database functions/connections
include("./db.php");
// connect to the database
dbConnect('kar_web1');
?>

<iframe id="stateFrame"
  name="stateFrame"
  style="width:00px; height:0px; border: 0px"
  src="blank.html"></iframe>

<body>
<?php
$FirstName= mysql_real_escape_string($_POST['FirstName']);
$LastName= mysql_real_escape_string($_POST['LastName']);
$PhoneNumber= mysql_real_escape_string($_POST['PhoneNumber']);
$EMail= mysql_real_escape_string($_POST['EMail']);
$HomeAddress = mysql_real_escape_string($_POST['HomeAddress']);
$userName = mysql_real_escape_string($_POST['userName']);
$EmergencyPhone = mysql_real_escape_string($_POST['EmergencyPhone']);
$catFoster = mysql_real_escape_string($_POST['catFoster']);
$dogFoster = mysql_real_escape_string($_POST['dogFoster']);
$name = $FirstName . " " . $LastName;
$pw1= mysql_real_escape_string($_POST['Password1']);
$pw2= mysql_real_escape_string($_POST['Password2']);

$query = "SELECT name FROM volunteers";
$result = mysql_query($query);
if (!$result)
{
	echo 'Could not run query: ' . mysql_error();
	exit;
}
// Initialize a check variable to 0
$check=0;

//CANNOT ALLOW DUPLICATE USERS!
while($namecheck = mysql_fetch_row($result))
{
	if($namecheck[0]==$name)
	{
		// User already exists, set the check to trip in next statement
		$check=1;
	}
}
// End of the while loop
if($check==1)
{
	// Let the user know they're already in
	echo "This name already exists in the database. If you need your password reset, ask an administrator.<br>";
	echo "<a href=\"index.php\">Back</a>";
}
else
// Proceed
{
	if($pw1==$pw2)
	{
		// Toss the password through an md5 hash function to encrypt
		$pw1 = md5($pw1);
		$userName = strtolower($userName);
		//ADD NEW USER!!!!!!
		$query = "INSERT INTO volunteers ( name, address, email, phone1, phone2, active, login, password, cat_foster, dog_foster)
		VALUES('$name', '$HomeAddress', '$EMail', '$PhoneNumber', '$EmergencyPhone', 'n', '$userName', '$pw1', '$catFoster', '$dogFoster')";
		mysql_query($query) or die (mysql_error());
	
		echo "<table width=100% border=0>";
		echo "<tr height=60px><td valign=top>";
			echo "<table width=100% height=30px border=0>";
				echo "<tr height=60px><td align=center valign=top>";
					echo "<font size=6 face=\"Verdana\">Registration Successful, please write down your user name and password, and keep it in a safe place!</font>";
					echo "<br><font size=3 face=\"Verdana\" color=\"red\">UserName= $userName</font>";
					echo "<br><font size=3 face=\"Verdana\" color=\"red\">Password= $pw2</font><br>";
					echo "<br><a href=\"login.php\">Login</a>";
				echo "</td></tr>";
			echo "</table>";
		echo "</td></tr>";
		echo "</table>";

	}
	else
	{
		echo "<tr><td valign=top align=center>";
		echo "<br>Passwords do not match, try again!<br>";
		echo "<br><a href=\"register.php\">Register</a>";
		echo "</td></tr>";
		echo "</table>";
	}
}
?>
</body>
